package task

import (
	"sync"
)

var (
	scheduleOnce   sync.Once
	scheduleSingle *Schedule
)

type Schedule struct {
	taskCh   chan *Task
	workSize int
}

func GetSchedule() *Schedule {
	scheduleOnce.Do(func() {
		scheduleSingle = &Schedule{
			taskCh:   make(chan *Task, 100),
			workSize: 10,
		}
		scheduleSingle.initSchedule()
	})
	return scheduleSingle
}

func (schedule *Schedule) initSchedule() {
	for i := 0; i < schedule.workSize; i++ {
		go schedule.work()
	}
}

func (schedule *Schedule) work() {
	for t := range schedule.taskCh {
		t.execTask()
	}
}

func (schedule *Schedule) SubmitTask(task *Task) {
	schedule.taskCh <- task
}
